import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		loginStatic: false,
		qxInfo: {},
	},
	mutations: {
		// 初始化登录状态 
		initUser(state) {
			let info = uni.getStorageSync('qxInfo')
			let loginStatic = uni.getStorageSync('loginStatic')
			if (info) {
				state.qxInfo = info
				state.loginStatic = true
			}
		},
		getUserInfo(state, data) {
			state.qxInfo = data
			state.loginStatic = true
			uni.setStorageSync('loginStatic', true)
			uni.setStorageSync('qxInfo', data)
		},
		// 更新用户昵称
		updateUserNickname(state, nickname) {
			if (state.qxInfo) {
				state.qxInfo.nickname = nickname
				uni.setStorageSync('qxInfo', state.qxInfo)
			}
		},
		// 更新用户头像
		updateUserAvatar(state, avatar) {
			if (state.qxInfo) {
				state.qxInfo.avatar = avatar
				uni.setStorageSync('qxInfo', state.qxInfo)
			}
		},
		// 更新用户信息（昵称和头像）
		updateUserProfile(state, { nickname, avatar }) {
			if (state.qxInfo) {
				if (nickname) state.qxInfo.nickname = nickname
				if (avatar) state.qxInfo.avatar = avatar
				uni.setStorageSync('qxInfo', state.qxInfo)
			}
		},
		logout(state) {
			// console.log('vuex退出登陆')
			state.loginStatic = false
			state.qxInfo = {}
			uni.removeStorageSync('loginStatic')
			uni.removeStorageSync('qxInfo')
		}
	},
	getters: {
		// 获取用户昵称
		userNickname: state => {
			return state.qxInfo && state.qxInfo.nickname ? state.qxInfo.nickname : ''
		},
		// 获取用户头像
		userAvatar: state => {
			return state.qxInfo && state.qxInfo.avatar ? state.qxInfo.avatar : ''
		},
		// 获取完整用户信息
		userInfo: state => {
			return state.qxInfo || {}
		}
	},
	actions: {

	}
})
export default store
